import excel "survey2019_truncated.xlsx", sheet("data") firstrow

**********************************
***** Survey-based variables *****
**********************************

gen edu = Q51

gen male = 0 
replace male = 1 if Gender=="Male"

gen age = .
replace age = 2018-Q1

gen edu_alt = edu
replace edu_alt = 1 if edu ==2
replace edu_alt = 1 if edu ==2
replace edu_alt = 2 if edu ==4
replace edu_alt = 3 if edu ==5
replace edu_alt = 3 if edu ==6

** 0 = no formal edu; 1 = some formal edu but no did not complete high school; 2 = high school graduate; 3 = beyond high school diploma

label define edu_alt 0 "No formal edu" 1 "Some formal edu" 2 "High school graduate" 3 "Beyond high school diploma"
label values edu_alt edu_alt

gen urban = 0 
replace urban =1 if WardVT == "Ward"

gen income = M5
label define income 0 "< 50,000" 1 "50,000 - 100,000" 2 "1 - 3 lakh" 3 "3 - 5 lakh" 4 "5 - 10 lakh" 5 "10 - 20 lakh" 6 ">20 lakh"
label values income income

gen minority = 1
replace minority = 0 if Q6==0

gen govservice = S4
replace govservice = . if S4==9

gen gadservice = S8
replace gadservice = . if S8==9

gen frespect = S9
replace frespect = . if S9==9

gen erespect = S11
replace erespect = . if S11==9

gen wunhtan = S13
replace wunhtan = . if S13==9

gen wunhtan_self = M7
replace wunhtan_self = 1 if M8==1

gen wunhtanfam = M9
replace wunhtanfam = 1 if wunhtan_self==1

gen buddhist = .
replace buddhist = 1 if Q7==0
replace buddhist = 0 if Q7>0

gen incumbent = M6

gen casualty = 0
replace casualty = 1 if M1 >0
replace casualty = 1 if M2 >0
replace casualty = . if M1 ==.
replace casualty = . if M1 ==.

gen visit = 0 
replace visit = 1 if S13<9
replace visit=. if S13==.

gen visit_gad = 0 
replace visit_gad = 1 if S8<9
replace visit_gad=. if S8==.

gen coethnic_CS = 0 if S16==0
replace coethnic_CS = 0 if S18==0
replace coethnic_CS = 1 if S16==1
replace coethnic_CS = 1 if S18==1


*** Relative nat pride
gen relativepride = A6-E6 if Q6>0
replace relativepride = . if A6==.
replace relativepride = . if E6==.

*** Attachment index
gen test4=0
replace test4= 1 if A1!=.
gen test5=0
replace test5= 1 if A4!=.
gen test6=0
replace test6= 1 if A6!=.
** number of attachment questions answered
gen test_attachment1 = test4+test5+test6
drop  test4 test5 test6

gen test4=A4
replace test4 = 0 if A1==.
gen test5=A5
replace test5 = 0 if A4==.
gen test6=A6
replace test6 = 0 if A6==.
* total attachment score
gen test_attachment2 = test4+test5+test6

gen attachment = test_attachment2/test_attachment1 if test_attachment1>1

drop test4 test5 test6 test_attachment2 test_attachment1



*** Respect + Satisfaction
gen test2=0
replace test2= 1 if wunhtan!=.
gen test3=0
replace test3= 1 if frespect!=.
** number of service questions answered
gen test_service = test2+test3

drop test2 test3


gen test2=wunhtan
replace test2 = 0 if wunhtan==.
gen test3=frespect
replace test3 = 0 if frespect==.

*** total service score
gen test_service1 = test2+test3

gen service = test_service1/test_service 
replace service = . if wunhtan==.
drop test2 test3 test_service test_service1



**********************************
***** Census-based variables *****
**********************************

** district capitols
gen district= 0
replace district = 1 if Township =="Gangaw" 
replace district = 1 if Township =="Pyin Oo Lwin" 
replace district = 1 if Township =="Lashio" 
replace district = 1 if Township =="Muse" 
replace district = 1 if Township =="Mongmit" 
replace district = 1 if Township =="Bhamo" 
replace district = 1 if Township =="Monyin" 
replace district = 1 if Township =="Myitkyina" 
replace district = 1 if Township =="Putao" 
replace district = 1 if Township =="Kalay" 
replace district = 1 if Township =="Tamu" 
replace district = 1 if Township =="Falam" 
replace district = 1 if Township =="Hakha" 
replace district = 1 if Township =="Matupi" 
replace district = 1 if Township =="Mindat" 


** Percent of township population for whom the Main Source of Lighting is Electricity. (Source 2014 Census data)
gen electricity_tsp = .
replace electricity_tsp = 0.41 if Township== "Bhamo"
replace electricity_tsp = 0.381 if Township== "Chipwi" 
replace electricity_tsp = 0.152 if Township== "Hpakant"
replace electricity_tsp = 0.173 if Township== "Machangbaw"
replace electricity_tsp = 0.251 if Township== "Mansi" 
replace electricity_tsp = 0.434 if Township== "Mogaung"
replace electricity_tsp = 0.257 if Township== "Monyin" 
replace electricity_tsp = 0.231 if Township== "Momauk"
replace electricity_tsp = 0.523 if Township== "Myitkyina" 
replace electricity_tsp = 0.091 if Township== "Putao" 
replace electricity_tsp = 0.192 if Township== "Tanai" 
replace electricity_tsp = 0.251 if Township== "Sumprabum" 
replace electricity_tsp = 0.128 if Township== "Waingmaw" 
replace electricity_tsp = 0.314 if Township== "Kutkai" 
replace electricity_tsp = 0.73 if Township== "Muse" 
replace electricity_tsp = 0.054 if Township== "Mongmit"
replace electricity_tsp = 0.531 if Township== "Lashio" 
replace electricity_tsp = 0.643 if Township== "Pyin Oo Lwin" 
replace electricity_tsp = 0.509 if Township== "Moegoke" 
replace electricity_tsp = 0.318 if Township== "Falam" 
replace electricity_tsp = 0.18 if Township== "Hakha" 
replace electricity_tsp = 0.041 if Township== "Mindat"
replace electricity_tsp = 0.11 if Township== "Matupi"
replace electricity_tsp = 0.121 if Township== "Kanpetlet" 
replace electricity_tsp = 0.188 if Township== "Tedim" 
replace electricity_tsp = 0.249 if Township== "Thantlang" 
replace electricity_tsp = 0.12 if Township== "Tonzang"  
replace electricity_tsp = 0.059 if Township== "Kalay" 
replace electricity_tsp = 0.169 if Township== "Gangaw" 
replace electricity_tsp = 0.133 if Township== "Tamu" 
replace electricity_tsp = 0.133 if Township== "Sidoktayar"
replace electricity_tsp = 0.168 if Township== "Ngape"
replace electricity_tsp = 0.281 if Township== "Aunglan"
replace electricity_tsp = 0.393 if Township== "Padaung" 
replace electricity_tsp = 0.129 if Township== "Paukkhaung" 


********************************************
***** Power-sharing and representation *****
********************************************

* Representation in Executive Cabinet
* Is there at least one co-ethnic in the president's cabinet? 
gen prescabinet = 0
replace prescabinet = 1 if Q6==0
replace prescabinet = 1 if Q6==1
replace prescabinet = 1 if Q6==5

*** REPRESENTATION AND POWERSHARING ***
** Representation in Pyithu Hluttaw
** Is the respondent's pyithu hluttaw rep a coethnic? 
gen phluttaw = 0
replace phluttaw = 1 if Township=="Mindat" & Q6==0
replace phluttaw = 1 if Township=="Kanpetlet" & Q6==1
replace phluttaw = 1 if Township=="Tonzang" & Q6==1
replace phluttaw = 1 if Township=="Thantlang" & Q6==1
replace phluttaw = 1 if Township=="Falam" & Q6==1
replace phluttaw = 1 if Township=="Matupi" & Q6==1
replace phluttaw = 1 if Township=="Hakha" & Q6==1
replace phluttaw = 1 if Township=="Kanpetlet" & Q6==1
replace phluttaw = 1 if Township=="Tedim" & Q6==1
replace phluttaw = 1 if Township=="Tedim" & Q61=="Zomi"
replace phluttaw = 1 if Township=="Kalay" & Q6==1
replace phluttaw = 1 if Township=="Gangaw" & Q6==0
replace phluttaw = 1 if Township=="Bhamo" & Q6==0
replace phluttaw = 1 if Township=="Mogaung" & Q6==0
replace phluttaw = 1 if Township=="Monyin" & Q6==0
replace phluttaw = 1 if Township=="Momauk" & Q6==0
replace phluttaw = 1 if Township=="Tanai" & Q6==1
replace phluttaw = 1 if Township=="Mansi" & Q6==1
replace phluttaw = 1 if Township=="Sumprabum" & Q6==2
replace phluttaw = 1 if Township=="Myitkyina" & Q6==2
replace phluttaw = 1 if Township=="Injangyang" & Q6==2
replace phluttaw = 1 if Township=="Chipwi" & Q6==2
replace phluttaw = 1 if Township=="Chipwi" & Q61=="Lachid"
replace phluttaw = 1 if Township=="Waingmaw" & Q6==2
replace phluttaw = 1 if Township=="Waingmaw" & Q61== "Lhavo"
replace phluttaw = 1 if Township=="Putao" & Q6==2
replace phluttaw = 1 if Township=="Putao" & Q61=="Lisu"
replace phluttaw = 1 if Township=="Machangbaw" & Q6==2
replace phluttaw = 1 if Township=="Machangbaw" & Q61=="Rawang"
replace phluttaw = 1 if Township=="Hpakant" & Q6==8 
replace phluttaw = 1 if Township=="Mongmit" & Q6==0
replace phluttaw = 1 if Township=="Kutkai" & Q6==2
replace phluttaw = 1 if Township=="Muse" & Q6==7
replace phluttaw = 1 if Township=="Lashio" & Q6==7
replace phluttaw = 1 if Township=="Pyin Oo Lwin" & Q6==0
replace phluttaw = 1 if Township=="Moegoke" & Q6==0
replace phluttaw = 1 if Township=="Ngape" & Q6==0
replace phluttaw = 1 if Township=="Aunglan" & Q6==0
replace phluttaw = 1 if Township=="Sidoktayar" & Q6==0
replace phluttaw = 1 if Township=="Padaung" & Q6==0
replace phluttaw = 1 if Township=="Paukkhaung" & Q6==0
*Source: Ethnicity coding comes from MYPILAR


** What portion of pyi-daung-su hluttaw is coethnic of the respondent? 
gen unionhluttaw = 0
replace unionhluttaw = 66.13  if Q6==0
replace unionhluttaw = 4.07   if Q6==1
replace unionhluttaw = 0.4007 if Q61=="Zomi"
replace unionhluttaw = 0.8146 if Q61=="Naga"
replace unionhluttaw = 3.46   if Q6==2
replace unionhluttaw = 0.4007 if Q61=="Lachid"
replace unionhluttaw = 0.4007 if Q61=="Lhavo"
replace unionhluttaw = 1.0183 if Q61=="Lisu"
replace unionhluttaw = 0.4007 if Q61=="Rawang"
replace unionhluttaw = 2.4440 if Q6==3
replace unionhluttaw = 4.6843 if Q6==4
replace unionhluttaw = 1.6293 if Q6==5
replace unionhluttaw = 6.110  if Q6==6
replace unionhluttaw = 6.5173 if Q6==7
replace unionhluttaw = 1.0183 if Q61=="Palaung"
replace unionhluttaw = 0.4073 if Q61=="Shanni"
replace unionhluttaw = 0.4073 if Q61=="Shan-Bamar"
*Source: Ethnicity coding comes from MYPILAR


************************
***** Reachability *****
************************

*Hard to reach towns May 2019. source: MIMU GIS data 
gen remote=0
replace remote= 1 if Township=="Falam" 
replace remote= 1 if Township=="Hakha" 
replace remote= 1 if Township=="Matupi" 
replace remote= 1 if Township=="Mindat" 
replace remote= 1 if Township=="Thantlang" 
replace remote= 1 if Township=="Tonzang"
replace remote= 1 if Township=="Tedim" 
replace remote= 1 if Township=="Kanpetlet" 
replace remote= 1 if Township=="Chipwi" 
replace remote= 1 if Township=="Machangbaw" 
replace remote= 1 if Township=="Sumprabum" 
replace remote= 1 if Township=="Putao" 


** average service at the locality level
sort locationid
by locationid: egen service_location = mean(service)

** income_alt missing imputed with (EM) mean 
gen income_alt = income 
replace income_alt = 1.742 if income ==. & Q6>0

** incumbent_alt missing imputed with (EM) mean 
gen incumbent_alt = incumbent
replace incumbent_alt = 1.611475   if incumbent ==. & Q6>0

** govservice_alt missing imputed with (EM) mean 
gen govservice_alt = govservice
replace govservice_alt = 1.284034   if govservice ==. & Q6>0

** conflict is on-going
gen conflict_ongoing = 0
replace conflict_ongoing = 1 if Q6==2
replace conflict_ongoing = 1 if Q6==6
replace conflict_ongoing = 1 if Q6==7

sort locationid
by locationid: egen govservice_location = mean(govservice)

sum service if Q6>0
gen service_pos = 0 if service < 1.720971 
replace service_pos = 1 if service >=  1.720971 
replace service_pos = . if service ==.

*****
rename A6 natpride
label define pride 0 "Not proud at all" 1 "Not proud" 2 "Somewhat proud" 3 "Very proud" 
label values natpride pride

rename Q6 ethnicity
label define ethnicity 0 "Bamar" 1 "Chin" 2 "Kachin" 3 "Kayah" 4 "Kayin" 5 "Mon" 6 "Rakhine" 7 "Shan" 8 "Other"
label values ethnicity ethnicity

rename A7 pen_myanmar

*** composite score of everyday encounter (respect regardless of ethnicity)
gen test2=0
replace test2= 1 if wunhtan!=.
gen test3=0
replace test3= 1 if erespect!=.
** number of service questions answered
gen test_service = test2+test3 if wunhtan!=.
** only seven didn't answer any question. 81% of the sample answered all three Qs. 15% answered two Qs and 4% answered just 1 Q.  

drop test2 test3


gen test2=wunhtan
replace test2 = 0 if wunhtan==.
gen test3=frespect
replace test3 = 0 if erespect==.

* total service score
gen test_service1 = test2+test3

gen service_erespect = test_service1/test_service 

drop test2 test3 test_service test_service1

** titular ethnic minoties
gen titular = 0 if ethnicity>0
replace titular = 1 if ethnicity==1 & State=="Chin"
replace titular = 1 if ethnicity==2 & State=="Kachin"
replace titular = 1 if ethnicity==7 & State=="Shan"

gen state = 0
replace state = 1 if State == "Kachin"
replace state = 1 if State == "Shan"
replace state = 1 if State == "Chin"


save "survey2019_truncated.dta", replace
